package com.stox.client.ui.javafx.chartingy.persistence.chart.view;

import javafx.beans.binding.ListBinding;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Bounds;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.TextField;
import javafx.scene.control.Tooltip;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;

import com.stox.client.api.core.Client;
import com.stox.client.ui.javafx.chartingy.persistence.chart.model.OChartGroup;
import com.stox.client.ui.javafx.chartingy.persistence.chart.widget.SavedChartListView;
import com.stox.client.ui.javafx.core.util.CSS;
import com.stox.client.ui.javafx.core.util.UI;
import com.stox.client.ui.javafx.widget.ConfigurationPopupControl;
import com.stox.client.ui.javafx.workbench.stage.StoxStage;
import com.stox.client.ui.javafx.workbench.view.View;
import com.stox.shared.charting.model.SavedChart;
import com.stox.shared.core.i18n.MessageSource;
import com.stox.shared.core.util.StringUtil;

public class SavedChartView extends View {

	private final ComboBox<OChartGroup> chartGroupComboBox = new ComboBox<OChartGroup>(OChartGroup.ALL);
	private final Button addButton = new Button(MessageSource.get("Add"));
	private final Button renameButton = new Button(MessageSource.get("Rename"));
	private final Button deleteButton = new Button(MessageSource.get("Delete"));
	private final HBox buttonsPanel = new HBox(addButton, renameButton, deleteButton);
	private final VBox chartGroupPanel = new VBox(chartGroupComboBox, buttonsPanel);
	private final SavedChartListView savedChartListView = new SavedChartListView(){};
	private final VBox container = new VBox(chartGroupPanel, savedChartListView);
	
	public SavedChartView(String titleText) {
		super(titleText);
		
		prepareChartGroupComboBox();
		prepareSavedChartListView();
		prepareAddButton();
		prepareRenameButton();
		prepareDeleteButton();
		
		setStyle();
		setSize();
		setLocation();
		setCenter(container);
	}
	
	private void setStyle(){
		getStyleClass().add(CSS.savedChartView);
		container.getStyleClass().add(CSS.savedChartViewContainer);
		chartGroupPanel.getStyleClass().add(CSS.chartGroupPanel);
		chartGroupComboBox.getStyleClass().add(CSS.chartGroupComboBox);
		buttonsPanel.getStyleClass().add(CSS.chartGroupButtonPanel);
		addButton.getStyleClass().add(CSS.addChartGroupButton);
		renameButton.getStyleClass().add(CSS.renameChartGroupButton);
		deleteButton.getStyleClass().add(CSS.deleteChartGroupButton);
	}
	
	private void prepareSavedChartListView(){
		savedChartListView.itemsProperty().bind(new ListBinding<SavedChart>(){
			
			{
				super.bind(chartGroupComboBox.valueProperty());
			}
			
			@Override
			protected ObservableList<SavedChart> computeValue() {
				OChartGroup chartGroup = chartGroupComboBox.getValue();
				if(null != chartGroup){
					if(chartGroup.getCharts().isEmpty()){
						chartGroup.setCharts(Client.CHARTING_CLIENT.findByChartGroupId(chartGroup.getId()));
					}
					return chartGroupComboBox.getValue().chartsProperty();
				}else{
					return FXCollections.emptyObservableList();
				}
			}
		});
	}
	
	private void prepareChartGroupComboBox(){
		chartGroupComboBox.setPrefWidth(Double.MAX_VALUE);
		OChartGroup.loadAll();
		if(!OChartGroup.ALL.isEmpty()){
			chartGroupComboBox.getSelectionModel().selectFirst();
		}
	}
	
	private void prepareAddButton(){
		addButton.setTooltip(new Tooltip(MessageSource.get("Add.Chart.Group")));
		addButton.setOnAction(new EventHandler<ActionEvent>(){
			@Override
			public void handle(ActionEvent event) {
				Pane pane = new Pane();
				TextField textField = UI.style(new TextField(), CSS.undecorated);
				pane.getChildren().add(textField);
				ConfigurationPopupControl configurationPopupControl = new ConfigurationPopupControl(pane);
				configurationPopupControl.getApplyButton().setOnAction(new EventHandler<ActionEvent>(){
					@Override
					public void handle(ActionEvent event) {
						String name = textField.getText();
						if(StringUtil.hasText(name)){
							OChartGroup chartGroup = new OChartGroup();
							chartGroup.setName(name);
							Client.CHARTING_CLIENT.save(chartGroup);
							OChartGroup.ALL.add(chartGroup);
							chartGroupComboBox.getSelectionModel().select(chartGroup);
						}else{
							
						}
					}
				});
				configurationPopupControl.show(StoxStage.INSTANCE);
			}
		});
	}
	
	private void prepareRenameButton(){
		renameButton.setTooltip(new Tooltip(MessageSource.get("Rename.Chart.Group")));
		renameButton.setOnAction(new EventHandler<ActionEvent>(){
			@Override
			public void handle(ActionEvent event) {
				Pane pane = new Pane();
				TextField textField = UI.style(new TextField(), CSS.undecorated);
				OChartGroup chartGroup = chartGroupComboBox.getSelectionModel().getSelectedItem();
				textField.setText(chartGroup.getName());
				pane.getChildren().add(textField);
				ConfigurationPopupControl configurationPopupControl = new ConfigurationPopupControl(pane);
				configurationPopupControl.getApplyButton().setOnAction(new EventHandler<ActionEvent>(){
					@Override
					public void handle(ActionEvent event) {
						String name = textField.getText();
						if(StringUtil.hasText(name) && !name.equals(chartGroup.getName())){
							chartGroup.setName(name);
							Client.CHARTING_CLIENT.save(chartGroup);
							chartGroupComboBox.getItems().set(chartGroupComboBox.getItems().indexOf(chartGroup), chartGroup);
							chartGroupComboBox.getSelectionModel().select(chartGroup);
						}else{
							
						}
					}
				});
				configurationPopupControl.show(StoxStage.INSTANCE);
			}
		});
	}
	
	private void prepareDeleteButton(){
		deleteButton.setTooltip(new Tooltip(MessageSource.get("Delete.Chart.Group")));
		deleteButton.setOnAction(new EventHandler<ActionEvent>(){
			@Override
			public void handle(ActionEvent event) {
				OChartGroup chartGroup = chartGroupComboBox.getSelectionModel().getSelectedItem();
				Client.CHARTING_CLIENT.deleteChartGroup(chartGroup.getId());
				chartGroupComboBox.getItems().remove(chartGroup);
			}
		});
	}
	
	private void setSize(){
		setPrefHeight(StoxStage.INSTANCE.getContentPane().getHeight()/2);
		setPrefWidth(StoxStage.INSTANCE.getContentPane().getWidth()/5);
	}
	
	private void setLocation(){
		Bounds bounds = StoxStage.INSTANCE.getContentPane().getLayoutBounds();
		setLayoutX(bounds.getMaxX() - getPrefWidth());
		setLayoutY(bounds.getMaxY() - getPrefHeight());
	}
}
